package cn.keniykey.mc.landterritory.toolkit;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import javax.annotation.Nullable;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public final class Util {
	private Util() {}
	
	public static String setStr(String str,final Object...objs) {
		for(int i = 0; i < objs.length; i++) str = str.replace("<" + i + ">", String.valueOf(objs[i]));
		return str;
	}
	public static String setStrs(final String oldmes,final String[] olds,final Object...news) {
		String newmes = oldmes;
		for(int a = 0; a < olds.length && a < news.length; a++) {
			newmes = newmes.replaceAll(olds[a], String.valueOf(news[a]));
		}
		return newmes;
	}
	/**
	 * 根据换行符\n分割文本
	 * @return ArrayList
	 */
	public static ArrayList<String> listFromStr(final String str) {
		if (str != null) {
			final String[] array = str.split("\\n");
			final ArrayList<String> list = new ArrayList<>(array.length);
			for(final String mes : array) list.add(mes);
			return list;
		}
		return new ArrayList<>();
	}
	/**
	 * 将String中的替换&为§
	 * 替换\n为换行符
	 */
	@Nullable
	public static String conver(final String str) {
		return (str == null ? str : str.replace('&', '§').replace("\\n ", "\n"));
	}
	/**
	 * conver功能替换&和\n
	 * 在原list基础上修改
	 */
	@Nullable
	public static List<String> conver(final List<String> list) {
		if (list == null) return list;
		final ListIterator<String> it = list.listIterator();
		while(it.hasNext()) {
			final String str = it.next();
			it.set(Util.conver(str));
		}
		return list;
	}
	public static Material getMaterial(final String name) {
		Material type;
		try {
			type = Material.matchMaterial(name);
			if (type == null) type = Material.STONE;
		}catch(Exception ex) {
			ex.printStackTrace();
			type = Material.STONE;
		}
		return type;
	}
	public static BlockFace getEntityLockFace(Entity en) {
		final Location loc = en.getLocation();
		float pitch = loc.getPitch();
		if (pitch < -50f) return BlockFace.UP;
		if (pitch > 50f) return BlockFace.DOWN;
		float yaw = (loc.getYaw() % 360f);
		if(yaw < 0f) yaw += 360f;
		if(yaw < 45f || yaw > 315f) return BlockFace.SOUTH;
		if(yaw >= 45f && yaw < 135f) return BlockFace.WEST;
		if(yaw >= 135f && yaw < 225f) return BlockFace.NORTH;
		return BlockFace.EAST;
	}
//	public static int getSpaceSurplus(final Inventory inv,final ItemStack stack) {
//		if (inv == null || stack == null) return 0;
//		int amount = 0;
//		for(final ItemStack item: inv.getStorageContents()) {
//			if (item == null || item.getType().equals(Material.AIR)) {
//				amount += stack.getMaxStackSize();
//				continue;
//			}
//			if (Util.comparison(stack, item)) amount += stack.getMaxStackSize() - item.getAmount(); 
//		}
//		return amount;
//	}
	public static int getVacancyAmount(final Inventory inventory) {
		int amount = 0;
		for(final ItemStack item : inventory.getStorageContents()) {
			if(item == null || Material.AIR.equals(item.getType())) amount++;
		}
		return amount;
	}
//	private static boolean comparison(final ItemStack item,final ItemStack stack) {
//		if (item == null || stack == null) return false;
//		if (item == stack) return true;
//		if (!item.getType().equals(stack.getType())) return false;
//        if (!item.getItemMeta().equals(stack.getItemMeta())) return false;
////        if (((Damageable)item.getItemMeta()).getDamage() != ((Damageable)stack.getItemMeta()).getDamage()) return false;
////        if (item.getItemMeta().hasDisplayName() || stack.getItemMeta().hasDisplayName()) {
////            if (!item.getItemMeta().hasDisplayName() || !stack.getItemMeta().hasDisplayName()) return false;
////            if (!item.getItemMeta().getDisplayName().equals(stack.getItemMeta().getDisplayName())) return false;
////        }
////        if (!item.getEnchantments().equals(stack.getEnchantments()))  return false;
////        final boolean book1 = item.getItemMeta() instanceof EnchantmentStorageMeta;
////        final boolean book2 = stack.getItemMeta() instanceof EnchantmentStorageMeta;
////        if (book1 != book2) return false;
////        if (book1) {
////            final Map<Enchantment, Integer> ench1 = (Map<Enchantment, Integer>)((EnchantmentStorageMeta)item.getItemMeta()).getStoredEnchants();
////            final Map<Enchantment, Integer> ench2 = (Map<Enchantment, Integer>)((EnchantmentStorageMeta)stack.getItemMeta()).getStoredEnchants();
////            if (!ench1.equals(ench2)) return false;
////        }
//        return true;
//	}
	public static String strSubToLine(final String str, final int length_line,@Nullable final String line_start_str) {
		final StringBuilder builder = new StringBuilder();
		int index = 0;
		int index_end = length_line;
		while(index_end < str.length()) {
			if(line_start_str != null && !line_start_str.isEmpty()) builder.append(line_start_str);
			builder.append(str.substring(index, index_end));
			builder.append("\n");
			index = index_end;
			index_end += length_line;
		}
		if(index < str.length() && index_end > str.length()) {
			if(line_start_str != null && !line_start_str.isEmpty()) builder.append(line_start_str);
			builder.append(str.substring(index, str.length()));
		}
		return builder.toString();
	}
}